﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Phoenix.Service;
using Phoenix.Service.Contracts;
using Phoenix.Service.Model;
using Phoenix.Common.Util;
using Phoenix.Enum;
namespace Phoenix.AdminSite.Controllers
{
    public class AdminUserRightController : ControllerBase
    {
        IAdminUserRightService IAdminUserRight = new AdminUserRightService();
        IRightsService IRights = new RightsService();
        IRightsTypeService IRightsType = new RightsTypeService();
        IIDCreaterService IIDCreater = new IDCreaterService();

        public ActionResult Index()
        {

            int userID = int.Parse(Request.QueryString["userID"]);

            Dictionary<int, int> dicRights = IAdminUserRight.GetRightIDs(userID);

            IList<RightsAndTypeModel> rightsAndTypeList = IRightsType.GetRightsAndType();

            for (int i = 0; i < rightsAndTypeList.Count; i++)
            {
                for (int j = 0; j < rightsAndTypeList[i].Rights.Count; j++)
                {
                    //存在 设为选中
                    if (dicRights.ContainsKey(rightsAndTypeList[i].Rights[j].RightID))
                    {
                        rightsAndTypeList[i].Rights[j].Checked = true;
                    }
                    else
                    {
                        rightsAndTypeList[i].Rights[j].Checked = false;
                    }
                }
            }

            ViewData["UserID"] = userID;

            ViewData["RightsAndType"] = rightsAndTypeList;

            return View();

        }

        public ActionResult Save(FormCollection collection)
        {
            IList<RightsModel> rightsList = IRights.GetAll();


            int userID = int.Parse(collection["hdUserID"]);

            IList<AdminUserRightModel> adminUserRightList = IAdminUserRight.GetByUserID(userID);

            foreach (var item in rightsList)
            {
                bool exists = false;
                int adminUserRightID = 0;

                foreach (var itemRights in adminUserRightList)
                {
                    if (itemRights.RightID == item.RightID)
                    {
                        exists = true;
                        adminUserRightID = itemRights.AdminUserRightID;
                        break;
                    }

                }

                if (!string.IsNullOrEmpty(collection["chkRight_" + item.RightID]))
                {



                    if (!exists)
                    {
                        AdminUserRightModel model= new AdminUserRightModel()
                        {
                            AdminUserRightID = IIDCreater.GetNewID("AdminUserRightID", "用户权限ID", 1000),
                            RightID = item.RightID,
                            UserID=userID
                        };
                        //不存在则添加权限数据
                        IAdminUserRight.AddAdminUserRight(model);
                    }
                }
                else
                {
                    if (exists)
                    {
                        IAdminUserRight.DeleteByAdminUserRightID(adminUserRightID);
                    }
                }
            }


            return ShowMsg("成功", Common.ActionStatus.OK, "");
        }

    }
}
